package com.fanshuai.algorithms.datastructure.graph;

public class Graph<T> implements IGraph<T> {
    private T[] nodes; //节点
    private int[][] matrix;  //邻接矩阵
    private int size;  //节点数量
    private boolean direct; //是否为有向图

    public Graph() {
        nodes = (T[]) new Object[256 + 1];
        matrix = new int[256 + 1][256 + 1];

        for (int i = 1; i <= 256; i++) {
            for (int j = 1; j <= 256; j++) {
                matrix[i][j] = Integer.MAX_VALUE;
            }
        }
        this.size = 256;
        this.direct = false;
    }

    public Graph(int size, boolean direct) {
        nodes = (T[]) new Object[size + 1];
        matrix = new int[size + 1][size + 1];

        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                matrix[i][j] = Integer.MAX_VALUE;
            }
        }
        this.size = size;
        this.direct = direct;
    }

    @Override
    public void addEdge(int i, int j, int weight) {
        matrix[i][j] = weight;
        if (!direct) {
            matrix[j][i] = weight;
        }
    }

    @Override
    public void delEdge(int i, int j) {
        matrix[i][j] = Integer.MAX_VALUE;
        if (!direct) {
            matrix[j][i] = Integer.MAX_VALUE;
        }
    }

    @Override
    public void setNodeInfo(int i, T node) {
        if (i <= 0 || i > size) {
            return;
        }

        nodes[i] = node;
    }

    @Override
    public T getNodeInfo(int i) {
        if (i <= 0 || i > size) {
            return null;
        }

        return nodes[i];
    }

    public int[][] getMatrix() {
        return matrix;
    }

    public int getSize() {
        return size;
    }
}
